<?php
declare (strict_types=1);

namespace app\controller\admin;

use app\controller\admin\Base;
use app\model\Promotion as OrderM;
use app\model\User as UserM;
use app\model\ReportUser as ReportUserM;
use think\annotation\route\Middleware;
use think\annotation\route\Group;
use think\annotation\Route;
use app\middleware\CheckAdmin;


/**
 * 统计
 * Class Report
 * @package app\controller\admin
 *
 * @Group("admin/report")
 * @Middleware({CheckAdmin::class})
 */
class Report extends Base
{
    /**
     * 用户推广统计
     * @Route("userReport", method="POST")
     */
    public function userR()
    {
        $page = input('page', 1, 'int');
        $goodsId = input('goodsId', 0, 'int');
        $name = input('name', '', 'string');
        $phone = input('phone', '', 'string');
        $onlyOne = input('onlyOne', 0, 'int'); // 如果为真，则只查询自己一级的
        $limit = input('limit', 50, 'int');
        if (empty($goodsId)) {
            return json_error(10002);
        }

        $role = $this->request->sys_user->role;
        $uid = $this->request->sys_user->id;
        if (empty($uid)) {
            return json_error(10002);
        }

        $where[] = array('goods_id', '=', $goodsId);
        $where[] = array('status', '=', 1);
        $userM = new UserM();
        $orderM = new OrderM();
        $list = array();
        if ($onlyOne) {
            $where[] = array('user_id', '=', $uid);
            if (!empty($name)) {
                $where[] = array('subordinate_name', '=', $name);
            }
            if (!empty($phone)) {
                $where[] = array('subordinate_account', '=', $phone);
            }
            $list = $orderM->where($where)->paginate(['page' => $page, 'list_rows' => $limit]);
            return json_ok($list,200);
        }
        if ($role == 0) {
            //管理员
            if (!empty($name)) {
                $where[] = array('subordinate_name', '=', $name);
            }
            if (!empty($phone)) {
                $where[] = array('subordinate_account', '=', $phone);
            }
            $list = $orderM->where($where)->paginate(['page' => $page, 'list_rows' => $limit]);
        } else {
            $userIds = $userM->where('id',$uid)->whereOr('parent_id', $uid)->column('id');
            if (!empty($userIds)) {
                $where[] = array('parent_user_id', 'in', $userIds);
                if (!empty($name)) {
                    $where[] = array('subordinate_name', '=', $name);
                }
                if (!empty($phone)) {
                    $where[] = array('subordinate_account', '=', $phone);
                }
                $list = $orderM->where($where)->paginate(['page' => $page, 'list_rows' => $limit]);
            }
        }
        return json_ok($list,200);
    }

    /**
     * 用户推广统计(下线）
     * @Route("userTwoReport", method="POST")
     */
    public function userTwoR()
    {
        $name = input('name', '', 'string');
        $phone = input('phone', '', 'string');
        $page = input('page', 1, 'int');
        $limit = input('limit', 50, 'int');

        $uid = $this->request->sys_user->id;
        if (empty($uid)) {
            return json_error(10002);
        }
        $where[] = array('puser_id', '=', $uid);
        if (!empty($name)) {
            $where[] = array('fx_user_name', '=', $name);
        }
        if (!empty($phone)) {
            $where[] = array('phone', '=', $phone);
        }
        $orderM = new ReportUserM();
        $list = $orderM->where($where)->paginate(['page' => $page, 'list_rows' => $limit]);
        return json_ok($list,200);
    }
}
